package com.yunfeng.supercall.httputils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Environment;

import com.ly.quickdev.library.imageutils.ImageDispose;
import com.yunfeng.supercall.httputils.UploadUtilsAsyncTask.OnUplaodListener;

public class HttpClient extends FinalHttp {

	// public static String COMMAN_SERVER_URL =
	// "http://tx.xtox.net:8078/api/Common/ComServer";
	// public static String NORMAL_SERVER_URL =
	// "http://tx.xtox.net:8078/api/API/";
	// public static String UPLOAD_IMG_URL =
	// "http://tx.xtox.net:8078/UploadImg/uploadFile.ashx";

	public static String COMMAN_SERVER_URL = "http://superchat.api.yunfengapp.com:8887/Common/ComServer";
	public static String NORMAL_SERVER_URL = "http://superchat.api.yunfengapp.com:8887/Super/";
	public static String UPLOAD_IMG_URL = "http://superchat.api.yunfengapp.com:8887/Controllers/uploadFile.ashx";

	public void post(String function, JSONObject json, MyAjaxCallBack callBack) {
		super.post(COMMAN_SERVER_URL, new MyAjaxParams(function, json),
				callBack);
	}

	public void post(String function, AjaxParams ajaxParams,
			MyAjaxCallBack callBack) {
		super.post(NORMAL_SERVER_URL + function, ajaxParams, callBack);
	}

	public void upLoadImg(final String path, final String imgname,
			final OnUplaodListener callBack) {
		new Thread() {
			public void run() {
				File file = new File(path);
				Options options = new Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeFile(path, options);
				options.inSampleSize = calculateInSampleSize(options, 1050,
						1500);
				options.inJustDecodeBounds = false;
				Bitmap bitmap = BitmapFactory.decodeFile(path, options);
				/**
				 * 获取图片的旋转角度，有些系统把拍照的图片旋转了，有的没有旋转
				 */
				int degree = ImageDispose.readPictureDegree(file
						.getAbsolutePath());
				/**
				 * 把图片旋转为正的方向
				 */
				bitmap = ImageDispose.rotaingImageView(degree, bitmap);
				file = saveMyBitmap(Environment.getExternalStorageDirectory()
						.getAbsolutePath() + "/.printphoto/" + imgname, bitmap);
				// AjaxParams ajaxParams = new AjaxParams();
				// try {
				// ajaxParams.put("file", file);
				// } catch (FileNotFoundException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				UploadUtilsAsyncTask mUploadUtils = new UploadUtilsAsyncTask(callBack);
				mUploadUtils.execute(file.getAbsolutePath(), UPLOAD_IMG_URL);
			};
		}.start();

	}

	public void sendPushMsg(int type, String title, String content,
			String toPhone) {
		AjaxParams mAjaxParams = new AjaxParams();
		mAjaxParams.put("MsgType", type + "");
		mAjaxParams.put("MsgData", content);
		mAjaxParams.put("AlertMsg", title);
		mAjaxParams.put("ToUserPhone", toPhone);
		super.post(NORMAL_SERVER_URL + "SendPushMsg", mAjaxParams,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub

					}
				});
	}
	public void sendPushMsg2(int type, String title, String content,
			String toPhone) {
		AjaxParams mAjaxParams = new AjaxParams();
		mAjaxParams.put("MsgType", type + "");
		mAjaxParams.put("MsgData", content);
		mAjaxParams.put("AlertMsg", title);
		mAjaxParams.put("ToUserPhone", toPhone);
		super.post(NORMAL_SERVER_URL + "SendPushMsg2", mAjaxParams,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub

					}
				});
	}

	/**
	 * Java文件操作 获取文件扩展名
	 * 
	 * Created on: 2011-8-2 Author: blueeagle
	 */
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot + 1);
			}
		}
		return filename;
	}

	public File saveMyBitmap(String bitName, Bitmap mBitmap) {
		File f = new File(bitName);
		try {
			f.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			mBitmap.recycle();
			mBitmap = null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return f;
	}

	// 计算图片的缩放值
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		final int height = options.outHeight;// 获取图片的高
		final int width = options.outWidth;// 获取图片的框
		int inSampleSize = 4;
		if (height > reqHeight || width > reqWidth) {
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;// 求出缩放值
	}
}
